package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.entity.Turbine;
import com.oceanread.cn.entity.Turbinelist;
import com.oceanread.cn.service.ITurbineService;
import com.oceanread.cn.service.ITurbinelistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/Fanlist")
@CrossOrigin()
@Api(value="风机Fanlist",tags={"4.1-风机列表"})
public class TurbinelistController {
    @Autowired
    private ITurbineService iTurbineService;
    @Autowired
    private ITurbinelistService iTurbinelistService;
    /*获取总表*/
    @ApiOperation(value = "前端可视化  获取总表")
    @GetMapping("/getall")
    public List<Turbinelist> getList() {
        return iTurbinelistService.getall();
    }

    @ApiOperation(value = "风机列表____分页")
    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iTurbinelistService.getTablelist(page, size));
        object.put("total", iTurbinelistService.getnum());
        return object;
    }

    @ApiOperation(value = "模糊查询____1参数+分页")
    @GetMapping("/query1")
    public JSONObject query(
            @RequestParam(name = "page", required = true, defaultValue = "1") int page,
            @RequestParam(name = "size", required = true, defaultValue = "10") int size,
            @RequestParam(name = "key",  defaultValue = "PrjCode") String key,
            @RequestParam(name = "value",defaultValue = "1") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iTurbinelistService.query(page,size,key,value));
        object.put("total", iTurbinelistService.getnumquery(key,value));
        return object;
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public Integer update(@RequestBody Turbinelist turbinelist) {
        Turbinelist turbinelist1=iTurbinelistService.equal("SYSID",String.valueOf(turbinelist.getSysid())).get(0);
        List<Turbine>turbines= iTurbineService.equal("PrjCode",turbinelist1.getPrjCode());
        for (Turbine turbine :turbines){
            turbine.setPrjCode(turbinelist.getPrjCode());
            turbine.setName(turbinelist.getName());
            iTurbineService.update(turbine);
        }
        return iTurbinelistService.update(turbinelist);
    }

    @ApiOperation(value = "插入")
    @PostMapping("/insert")
    public Integer insert(@RequestBody Turbinelist turbinelist) {
        return iTurbinelistService.insert(turbinelist.getPrjCode(),turbinelist.getName(),
                turbinelist.getNum(),turbinelist.getRegion(),turbinelist.getDatetime(),
                turbinelist.getLon(),turbinelist.getLat(),turbinelist.getHeight());
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "SYSID") int sysid) {
        return iTurbinelistService.delete(sysid);
    }
}
